package com.ts.api.module.api.logger;

import com.gitee.apanlh.util.date.DateUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.ts.api.common.constant.productflow.ProductApplyFlowSrcTypeEnum;
import com.ts.api.common.constant.productflow.ProductApplyFlowStatusEnum;
import com.ts.api.common.context.ApiContext;
import com.ts.api.common.localcache.DistrictCache;
import com.ts.api.module.api.entity.po.ApiKeyPO;
import com.ts.api.module.common.entity.DistrictPO;
import com.ts.api.module.product.entity.po.ProductApplyFlowPO;
import com.ts.api.module.product.entity.po.ProductPlanPO;

/**
 *  API流程-主体计划分发流程日志记录器
 *
 *  @author Pan
 */
public class ApiFlowLogger {

    /**
     *  创建流程日志
     *
     *  @param  apiKeyPO            API渠道实体
     *  @param  finalMatchPlanPO    最终匹配计划实体
     *  @param  cityName            城市名称
     *  @param  flowTag             流程标识
     *  @param  flowStatusEnum      流程状态
     *  @return ProductApplyFlowPO
     */
    public static ProductApplyFlowPO createFlow(ApiKeyPO apiKeyPO, ProductPlanPO finalMatchPlanPO, String flowTag, String cityName, ProductApplyFlowStatusEnum flowStatusEnum) {
        String reqId = ApiContext.REQ_ID.get();
        ProductApplyFlowPO productApplyFlowPO = new ProductApplyFlowPO();
        productApplyFlowPO.setApiClientId(apiKeyPO.getApiClientId());
        productApplyFlowPO.setLoanChannelId(apiKeyPO.getLoanChannelId());
        productApplyFlowPO.setReqId(ClassConvertUtils.toLong(reqId));
        productApplyFlowPO.setSrcType(ProductApplyFlowSrcTypeEnum.API.getType());
        productApplyFlowPO.setStatus(flowStatusEnum.getType());
        productApplyFlowPO.setPhoneMd5(flowTag);
        productApplyFlowPO.setCreateTime(DateUtils.currentTime());
        productApplyFlowPO.setLoanProductId(finalMatchPlanPO.getLoanProductId());
        productApplyFlowPO.setLoanProductPlanId(finalMatchPlanPO.getId());
        DistrictPO districtPO = DistrictCache.getByName(cityName);
        productApplyFlowPO.setAreaCode(districtPO.getCode());
        productApplyFlowPO.setAreaName(districtPO.getName());
        return productApplyFlowPO;
    }

    /**
     *  创建手机号掩码流程日志
     *
     *  @param  apiKeyPO            API渠道实体
     *  @param  cityName            城市名称
     *  @param  flowTag             流程标识
     *  @param  flowStatusEnum      流程状态
     *  @return ProductApplyFlowPO
     */
    public static ProductApplyFlowPO createPhoneMaskFlow(ApiKeyPO apiKeyPO, String flowTag, String cityName, ProductApplyFlowStatusEnum flowStatusEnum) {
        String reqId = ApiContext.REQ_ID.get();
        ProductApplyFlowPO productApplyFlowPO = new ProductApplyFlowPO();
        productApplyFlowPO.setApiClientId(apiKeyPO.getApiClientId());
        productApplyFlowPO.setLoanChannelId(apiKeyPO.getLoanChannelId());
        productApplyFlowPO.setReqId(ClassConvertUtils.toLong(reqId));
        productApplyFlowPO.setSrcType(ProductApplyFlowSrcTypeEnum.API.getType());
        productApplyFlowPO.setStatus(flowStatusEnum.getType());
        productApplyFlowPO.setPhoneMd5(flowTag);
        productApplyFlowPO.setCreateTime(DateUtils.currentTime());
        DistrictPO districtPO = DistrictCache.getByName(cityName);
        productApplyFlowPO.setAreaCode(districtPO.getCode());
        productApplyFlowPO.setAreaName(districtPO.getName());
        return productApplyFlowPO;
    }
}
